﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.MathUtilitys;

namespace Utils.Layouting
{
    public class VerticalDevider : BasePlane
    {
        struct ChildEntry
        {
            public BasePlane Child;
            public int Weight;

            public ChildEntry(BasePlane child, int weight)
            {
                this.Child = child;
                this.Weight = weight;
            }
        }

        List<ChildEntry> childs = new List<ChildEntry>();

        public override void UpdateSize(FRect newRect)
        {
            base.UpdateSize(newRect);

            float totalWeight = 0;
            foreach (ChildEntry child in childs)
            {
                totalWeight += child.Weight;
            }

            if (totalWeight > 0)
            {
                float currentWeight = -totalWeight / 2f;
                foreach (ChildEntry child in childs)
                {
                    if (child.Child != null)
                    {
                        child.Child.UpdateSize(new FRect(
                            rect.X, rect.Y + (currentWeight + child.Weight / 2f) * rect.Height / totalWeight,
                            rect.Width, rect.Height * child.Weight / totalWeight));
                    }
                    currentWeight += child.Weight;
                }
            }
        }

        public void AddChild(BasePlane child, int weight)
        {
            childs.Add(new ChildEntry(child, weight));
        }
    }
}
